package com.beeway.blackgoose.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.beeway.blackgoose.R;
import com.beeway.blackgoose.adapter.CouponViewPagerAdapter;
import com.beeway.blackgoose.constants.Config;
import com.beeway.blackgoose.fragment.CouponAllFragment;
import com.beeway.blackgoose.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;


public class CouponActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, CompoundButton.OnCheckedChangeListener {
    @ViewInject(R.id.coupon_contanrs_rgp)
    private RadioGroup couponRgs;
    private List<Fragment> couponFragments;
    private CouponViewPagerAdapter couponAdapter;
    @ViewInject(R.id.vp_coupon_contanrs)
    private ViewPager couponViewPager;
    private RadioButton[] rbs;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        couponFragments = new ArrayList<>();
        couponFragments.add(CouponAllFragment.getInstance(Config.COUPON_ALL));
        couponFragments.add(CouponAllFragment.getInstance(Config.COUPON_OVERDUE));
        couponFragments.add(CouponAllFragment.getInstance(Config.COUPON_USED));
        couponFragments.add(CouponAllFragment.getInstance(Config.COUPON_OVERDUE));
        couponAdapter = new CouponViewPagerAdapter(getSupportFragmentManager(), couponFragments);
        couponViewPager.setAdapter(couponAdapter);
        couponViewPager.addOnPageChangeListener(this);
        getRadioButton();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        rbs[position].setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void getRadioButton() {
        rbs = new RadioButton[couponRgs.getChildCount()];
        for (int i = 0; i < rbs.length; i++) {
            rbs[i] = (RadioButton) couponRgs.getChildAt(i);
            rbs[i].setOnCheckedChangeListener(this);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (rbs[0].isChecked()) {
            couponViewPager.setCurrentItem(0);
        } else if (rbs[1].isChecked()) {
            couponViewPager.setCurrentItem(1);
        } else if (rbs[2].isChecked()) {
            couponViewPager.setCurrentItem(2);
        } else if (rbs[3].isChecked()) {
            couponViewPager.setCurrentItem(3);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Utils.getAnimOut(this);
    }
}
